﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Text;
namespace Golf.WebUI.GolfCart
{
    public partial class InitCart : System.Web.UI.Page
    {
        public string ProId
        {
            get
            {
                if (Request.QueryString["pro_Id"] != null && Request.QueryString["pro_Id"] != "")
                {
                    return Request.QueryString["pro_Id"].ToString().Trim();
                }
                else
                {
                    return null;
                }
            }
        }
        public string SkuId
        {
            get
            {
                if (Request.QueryString["skuId"] != null && Request.QueryString["skuId"] != "")
                {
                    return Request.QueryString["skuId"].ToString().Trim();
                }
                else
                {
                    return null;
                }
            }
        }
        public int Quantity
        {
            get
            {
                if (Request.QueryString["quantity"] != null && Request.QueryString["quantity"] != "")
                {
                    return Convert.ToInt32(Request.QueryString["quantity"].ToString().Trim());
                }
                else
                {
                    return 0;
                }
            }
        }
        Golf.BLL.Golf_Product bllproduct = new Golf.BLL.Golf_Product();
        Golf.Model.Golf_Product modelproduct = new Golf.Model.Golf_Product();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (ProId != null && ProId != "")
                {
                    modelproduct = bllproduct.GetModel(int.Parse(ProId));
                    ShopCartAdd(modelproduct.Id, SkuId, Quantity);
                    Response.Redirect("Cart.aspx");
                }
            }
        }

        /// <summary>
        /// 添加商品进入购物车
        /// </summary>
        /// <param name="id">商品</param>
        /// <param name="skuid">属性ids</param>
        /// <param name="buynum">购买数量</param>
        private void ShopCartAdd(int id, string skuid, int buynum)
        {
            //CookiesHelper.ClearCookie("GOLF");
            //HttpCookie dfdf = Request.Cookies["GOLF"];
            //Response.Write(string.Concat(dfdf.Values.AllKeys));
            //return;

            //同一商品不同属性”id_skuid“保存键值
            string[] _skuid = skuid.Split(',');
            StringBuilder sb = new StringBuilder();
            sb.Append(ProId);
            foreach (var item in _skuid)
            {
                sb.Append("_" + item);
            }
            //string _key = ProId + "_" + string.Concat(_skuid);
            string _key = sb.ToString();
            if (Request.Cookies["GOLF"] == null)
            {
                Response.Cookies["GOLF"][_key] = id + "|" + buynum + "|" + Get_pro_attr(skuid) + "|" + skuid;
                Response.Cookies["GOLF"].Expires = DateTime.Now.AddDays(30);
            }
            else
            {
                HttpCookie ShopCart = Request.Cookies["GOLF"];
                int _num = 0;
                if (ShopCart[_key] != null && ShopCart[_key].Trim() != "")//重复购买
                {
                    int.TryParse(ShopCart[_key].Split('|')[1], out _num);
                    _num += buynum;
                    ShopCart[_key] = null;
                }
                else  //第一次购买
                {
                    _num = buynum;
                }

                ShopCart[_key] = id + "|" + _num + "|" + Get_pro_attr(skuid) + "|" + skuid;
                ShopCart.Expires = DateTime.Now.AddDays(30);
                Response.Cookies.Add(ShopCart);
            }
        }
        /// <summary>
        /// 更加 添加到购物车的商品的属性 获取属性键值对
        /// </summary>
        /// <param name="skuid">属性id编号 XX,XX,XX</param>
        /// <returns>返回属性：值</returns>
        private string Get_pro_attr(string skuid)
        {
            /*8、String user_IP=Request.ServerVariables["REMOTE_ADDR"].ToString(); 
取远程用户IP地址 

9、穿过代理服务器取远程用户真实IP地址： 
if(Request.ServerVariables["HTTP_VIA"]!=null){ 
string user_IP=Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); 
}else{ 
string user_IP=Request.ServerVariables["REMOTE_ADDR"].ToString(); 
}  */
            Golf.BLL.Golf_Attribute bll_attr = new Golf.BLL.Golf_Attribute();
            List<Golf.Model.Golf_Attribute> list_attr = bll_attr.GetModelList("id in(" + skuid + ")");
            StringBuilder sb = new StringBuilder();
            foreach (var item in list_attr)
            {
                Golf.Model.Golf_Attribute model = bll_attr.GetModel((int)item.ParentID);
                if (model != null)
                {
                    sb.Append("<p>" + model.AttrParent + ":<span>" + item.AttrChild + "<span></p>");
                }
            }
            return Server.UrlEncode(sb.ToString());
        }

    }
}
